#include "tile.h"

#define PI 3.14159265

#include "gamestate.h"
#include "resourcemanager.h"

#include "entities/improvement.h"
#include "entities/terraintype.h"
#include "entities/terrainfeature.h"

Tile::Tile(int index):Entity(){
    this->index = index;
    selected = false;
    r = 0.1;
    g = 0.35;
    b = 0.1;
    ResetPathfinding();

    improvement = -1;
    city_owner = -1;
	owner = -1;
    roadType = "none";
    distanceToFreshWater = 100;
    terrainFeatureName = "none";
    terrainTypeName = "none";

    terrain_food = 0;
}

void Tile::SetTerrain(std::string terrain_name, std::string feature_name){
    // Default values are zero for everything
    TerrainType* terrain = resourceManager->GetTerrainType(terrain_name);
    TerrainFeature* feature = resourceManager->GetTerrainFeature(feature_name);

    terrainTypeName = terrain->GetName();
    terrainFeatureName = feature->GetName();

    movementCost = terrain->GetMovementCost()+feature->GetMovementCost();
    centerHeight = feature->GetHeightmapValue(12);

    terrain_food = terrain->GetFood();
}

int Tile::GetFood(){
    return terrain_food;
}

void Tile::ResetPathfinding(){
	F = 0;
	G = 0;
	H = 0;
	ParentDirection = NOPARENT;
	ParentID = -1;
}

bool Tile::DiagonalParent(){
	if(ParentDirection == ABOVERIGHT ||
	   ParentDirection == ABOVELEFT ||
	   ParentDirection == BELOWRIGHT ||
	   ParentDirection == BELOWLEFT)
		return true;
	else
		return false;
}

void Tile::SetMinimumDistanceToFreshWater(float dist){
    if(distanceToFreshWater > dist){
        distanceToFreshWater = dist;
        if(dist<=1.5){
            SetTerrain("grassland");
        }
        else if(dist<=2.95){
            SetTerrain("plains");
        }
    }
}

